package com.five.controller;

import com.five.dto.NovelEsDTO;
import com.five.esearch.impl.NovelDataSyncServiceImpl;
import com.five.esearch.impl.NovelSearchServiceImpl;
import com.five.pojo.Novel;
import com.five.query.NovelSearchQuery;
import com.five.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小说搜索接口
 */
@RestController
@RequestMapping("/reader/search")
public class SearchController {

    private final NovelSearchServiceImpl searchService;

    @Autowired
    private NovelDataSyncServiceImpl novelDataSyncServiceImpl;

    @Autowired
    public SearchController(NovelSearchServiceImpl searchService) {
        this.searchService = searchService;
    }

    /**
     * 小说搜索接口
     * @param query 搜索参数（keyword、pageNum、pageSize等）
     * @return 搜索结果
     */
    @PostMapping("/novel")
    public Result<List<NovelEsDTO>> searchNovel(@RequestBody NovelSearchQuery query) {
        return searchService.search(query);
    }
    /*
    * 全量同步小说数据
     */
    @PostMapping("/sync/full")
    public Result<String> syncNovel() {
        try {
            novelDataSyncServiceImpl.fullSync();
            return Result.success("同步成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("同步失败：" + e.getMessage());
        }
    }

}